package com.kgc.mall.mapper;

import com.kgc.mall.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {
    /**
     *按多个 条件分页查询用户信息列表
     * @param startNo  页码
     * @param pageSize 页大小
     * @param loginName 登录名
     * @param realName 真实姓名
     * @return 返回用户实体对象集合
     */
    @Select("<script>SELECT * from `user` "
            + "WHERE 1=1 "
            + "<if test='loginName != null and loginName.trim().length() >0'>"
            + " and loginName=#{loginName}"
            + "</if>"
            + "<if test='realName != null and realName.trim().length() >0'>"
            + " and userName like CONCAT('%',#{realName},'%')"
            + "</if>"
            + " ORDER BY id ASC "
            + " LIMIT #{startNo},#{pageSize} "
            + "</script>")
    List<User> getUsersByPaging(
            @Param("startNo") int startNo,
            @Param("pageSize") int pageSize,
            @Param("loginName") String  loginName,
            @Param("realName") String realName //UserQueryParamDTO

    );

    /**
     * 按多个条件查询用户总记录
     * @param loginName
     * @param realName
     * @return
     */
    @Select("<script> select count(*) from `user`"
            + "<if test='loginName != null and loginName.trim().length() >0'>"
            + " and loginName like CONCAT('%',#{loginName})"
            +"</if>"
            + "<if test='realName != null and realName.trim().length() >0'>"
            + " and userName like CONCAT('%',#{realName},'%')"
            + "</if>"
            +" </script>")
    long getUserTotalCount(
            @Param("loginName") String loginName,
            @Param("realName") String  realName
    );

    /**
     * 根据用户查找用户信息
     * @param id
     * @return
     */
    @Select("<script> select * from `user` where `id`=#{id} </script>")

    User getUser (int id);

    /** q
     * 通过用户 id 列表 查找用户信息
     * @param userIds
     * @return
     */
//    @Select(" select * from `user` where 1=1 and id in <foreach item='id' collection='list' open='('  separator=',' close=')'> #{id}</foreach>")
    @Select("<script>"+"SELECT * from `user` "
            + "WHERE 1=1 "
            +" AND id in"
            + "<foreach collection='list' open='(' separator=',' close=')' item='id'>"
            + "#{id}"
            + "</foreach>"
            + "</script>")
    List<User> getUserByIdS(@Param("userIds") List<Integer> userIds);

    /**
     *根据用户的id 修改指定用户的信息
     * @param user 用户实体对象
     * @return
     */
    @Update(" update `user` set loginName=#{loginName}," +
            " userName=#{userName},password=#{password}," +
            "identityCode=#{identityCode},sex=#{sex} ," +
            "mobile=#{mobile} ,email=#{email}, type=#{type} where id=#{id}")
    int updateUser(User user);

    /**
     * 根据用户的id删除用户信息
     * @param UserId
     * @return
     */
    @Delete("<script> delete from `user` where id=#{UserId}</script>")
    int delectUser( int UserId);

    /**
     * 批量更新用户信息
     * @param userList
     * @return
     */
   @Update("<foreach item='item' collection='list' index='index' separator=','> update `user` set loginName=#{item.loginName}," +
           "userName=#{item.userName},password=#{item.password}," +
           "identityCode=#{item.identityCode},sex=#{item.sex} ,"+
           "mobile=#{item.mobile} ,email=#{item.email} ,type=#{item.type} where id=#{item.id}" +
           "</foreach>")
    int updateBatch( List<User> userList);

    /**
     * 批量删除
     * @param userId
     * @return
     */
    @Delete("<script>" +
            "delete from `user` where id in " +
            "<foreach collection='collection' open='(' item='id' separator=',' close=')'> #{id}" +
            "</foreach>" +
            "</script>")
    int deleteBactch( List<Integer> userId);



    @Insert("<script> insert into `user` (id,loginName, userName,password,sex,identityCode,email,mobile,type) values" +
            "<foreach item='item'  collection='collection' separator=','>" +
            "(#{item.id} ,#{item.loginName},#{item.userName},#{item.password},#{item.sex},#{item.identityCode},#{item.email},#{item.mobile},#{item.type})" +
            " </foreach></script>")
    int insertBactch(List<User> userList);


}
